<template>
  <el-row class="home" :gutter="10">
    <el-col :span="8" style="margin-top: 10px; ">
      <el-card shadow="hover" style="border-radius: 10px;">
        <div class="user">
          <img src="../../assets/images/user.jpg">
          <div class="user-info">
            <p class="name">Admin</p>
            <p class="role">超级管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间:<span>2024-7-22</span></p>
          <p>上次登录的地点:<span>杭州</span></p>
        </div>
      </el-card>
      <el-card shadow="hover" style="margin-top: 10px;border-radius: 20px;" height="450px">
        <el-table :data="tableData">
          <el-table-column
              v-for="(val, key) in tableLabel"
              :key="key"
              :prop="key"
              :label="val"
          >
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16" style="margin-top: 10px;" class="right-num">
      <el-card shadow="hover" style="border-radius: 10px">
        <div class="num">
          <el-card
              :body-style="{ display: 'flex', padding: 0 }"
              v-for="item in countData"
              :key="item.name"
          >
            <i
                class="icon"
                :class="`el-icon-${item.icon}`"
                :style="{ background: item.color }"
            ></i>
            <div class="detail">
              <p class="price">￥{{ item.value }}</p>
              <p class="desc">{{ item.name }}</p>
            </div>
          </el-card>
        </div>
          <el-card style="height: 280px;">
            <!-- 折线图 -->
            <div ref="echarts1" style="height: 280px"></div>
          </el-card>
        <div class="graph">
          <el-card style="height: 240px">
            <div ref="echarts2" style="height: 240px"></div>
          </el-card>
          <el-card style="height: 240px">
            <div ref="echarts3" style="height: 220px"></div>
          </el-card>
        </div>
      </el-card>
    </el-col>
  </el-row>
</template>

<script>
import {courseInfo,graphicData} from '@api/home'
import * as echarts from 'echarts'

export default {
  data() {
    return {
      tableLabel: {
        name: "课程",
        todayBuy: "今日购买",
        monthBuy: "本月购买",
        totalBuy: "总购买",
      },
      tableData: [],
      countData: [
        {
          name: "今日支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "今日收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "今日未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
        {
          name: "本月支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "本月收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "本月未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
      ],
      tableSize: 5
    }
  },
  methods: {
    getCourseInfo() {
      courseInfo(this.tableSize).then((res) => {
        if (res.code === 200) {
          this.tableData = res.data;
        } else {
          this.$message.error(res.data.message);
        }
      })
    }
  },
  created() {
    this.getCourseInfo()
  },
  mounted() {
    graphicData().then((res) => {
      let legenddata,xAxisdata,seriesData;
      if (res.code === 200) {
        legenddata = res.data.orderData.legendData;
        xAxisdata = res.data.orderData.xaxisData;
        seriesData = res.data.orderData.series;
        var chartDom = this.$refs.echarts1;
        var myChart = echarts.init(chartDom);
        var option;
        // 添加折线图需要的数据
        seriesData.forEach(item=> {
          item.type='line';
          item.stack='Total'
        })
        // 折线图
        option = {
          title: {
            text: '订单信息'
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: legenddata
          },
          toolbox: {
            feature: {
              saveAsImage: {}
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: xAxisdata
          },
          yAxis: {
            type: 'value'
          },
          series: seriesData
        };
        myChart.setOption(option);
        // 柱状图
          let userData = res.data.userData;
        const echarts2 = echarts.init(this.$refs.echarts2)
        const eachrts2Option = {
          legend: {
            // 图例文字颜色
            textStyle: {
              color: "#333",
            },
          },
          grid: {
            left: "20%",
          },
          // 提示框
          tooltip: {
            trigger: "axis",
          },
          xAxis: {
            type: "category", // 类目轴
            data: userData.map(item => item.date),
            axisLine: {
              lineStyle: {
                color: "#17b3a3",
              },
            },
            axisLabel: {
              interval: 0,
              color: "#333",
            },
          },
          yAxis: [
            {
              type: "value",
              axisLine: {
                lineStyle: {
                  color: "#17b3a3",
                },
              },
            },
          ],
          color: ["#2ec7c9", "#b6a2de"],
          series: [
            {
              name: '新增用户',
              data: userData.map(item => item.num),
              type: 'bar'
            },
            {
              name: '活跃用户',
              data: userData.map(item => item.active),
              type: 'bar'
            }
          ],
        }
        echarts2.setOption(eachrts2Option)

        // 饼状图
        let videoData = res.data.videoData;
        const echarts3 = echarts.init(this.$refs.echarts3)
        const eachrts3Option = {
          tooltip: {
            trigger: "item",
          },
          color: [
            "#0f78f4",
            "#dd536b",
            "#9462e5",
            "#a6a6a6",
            "#e1bb22",
            "#39c362",
            "#3ed1cf",
          ],
          series: [
            {
              data: videoData,
              type: 'pie'
            }
          ],
        }
        echarts3.setOption(eachrts3Option)
      } else {
        this.$message.error(res.data.message);
      }
    })

  }
}
</script>

<style lang="less" scoped>
.home {
  // 样式表中的 element.style样式如何修改 样式表中的 element.style样式如何修改
  margin-left: 10px !important;
  margin-right: 10px !important;

  .user {
    display: flex;
    // 元素定位对齐的方式
    align-items: center;
    // 下内边
    padding-bottom: 20px;
    // 下边距
    border-bottom: 1px solid #ccc;
    // 下外距
    margin-bottom: 20px;

    .user-info {
      p {
        // 多行文本间的间距
        line-height: 30px;
        // 文本的大小
        font-size: 20px;
        // 颜色
        color: #666666;
      }
    }

    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
      margin-right: 40px;
    }
  }

  .login-info {
    p {
      // 多行文本的边距
      line-height: 30px;
      // 字体的大小
      font-size: 14px;
      // 字体颜色
      color: #758eb5;

      span {
        // 字体颜色
        color: #666;
        // 左外边距
        margin-left: 60px;
      }
    }
  }

  .num {
    // 浮动起来
    display: flex;
    // CSS 的 flex-wrap 属性指定 flex 元素单行显示还是多行显示。如果允许换行，这个属性允许你控制行的堆叠方向。
    flex-wrap: wrap;
    //CSS justify-content 属性定义浏览器如何沿着弹性容器的主轴和网格容器的行向轴分配内容元素之间和周围的空间。
    justify-content: space-between;
    .icon{
      width: 80px;
      height: 80px;
      font-size: 30px;
      // text-align CSS 属性设置块元素或者单元格框的行内内容的水平对齐。这意味着其效果和 vertical-align 类似，但是是水平方向的。
      text-align: center;
    //  line-height CSS 属性用于设置多行元素的空间量，如多行文本的间距。对于块级元素，它指定元素行盒（line boxes）的最小高度。对于非替代的 inline 元素，它用于计算行盒（line box）的高度。
      line-height: 80px;
      // 文字颜色
      color: #fff;
    }
    .detail{
      margin-left: 10px;
      display: flex;
      //flex-direction 属性指定了内部元素是如何在弹性容器中布局的，定义了主轴的方向（正方向或反方向）。
      flex-direction: column;
      justify-content: center;
      .price{
        font-size: 30px;
        margin-bottom: 10px;
        line-height: 20px;
        height: 30px;
      }
      .desc{
        font-size: 15px;
        color: #999;
        text-align: center;
      }
    }
    .el-card {
      width: 32%;
      margin-bottom: 20px;
    }
  }
  .graph{
    display: flex;
    margin-top: 20px;
    justify-content: space-between;
    .el-card {
      width: 48%;
    }
  }
}


</style>
